use serde::{Deserialize, Serialize};
use uuid::Uuid;
use chrono::{DateTime, Utc};
use std::collections::HashMap;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Connection {
    pub id: Uuid,
    pub name: String,
    pub description: Option<String>,
    pub source_id: Uuid,
    pub destination_id: Uuid,
    pub configuration: ConnectionConfig,
    pub status: ConnectionStatus,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConnectionConfig {
    pub sync_mode: SyncMode,
    pub schedule: Option<String>,
    pub transformations: Vec<Transformation>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SyncMode {
    FullRefresh,
    Incremental,
    RealTime,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ConnectionStatus {
    Active,
    Paused,
    Error,
    Syncing,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Connector {
    pub id: Uuid,
    pub name: String,
    pub connector_type: ConnectorType,
    pub specification: ConnectorSpec,
    pub configuration: HashMap<String, serde_json::Value>,
    pub status: ConnectorStatus,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ConnectorType {
    Source,
    Destination,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ConnectorStatus {
    Active,
    Inactive,
    Error,
    Testing,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConnectorSpec {
    pub name: String,
    pub version: String,
    pub description: String,
    pub connection_specification: serde_json::Value,
    pub supported_sync_modes: Vec<SyncMode>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Transformation {
    pub id: Uuid,
    pub name: String,
    pub transformation_type: TransformationType,
    pub configuration: serde_json::Value,
    pub order: i32,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum TransformationType {
    Filter,
    Map,
    Aggregate,
    Join,
    Custom,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Pipeline {
    pub id: Uuid,
    pub name: String,
    pub description: Option<String>,
    pub connections: Vec<Uuid>,
    pub status: PipelineStatus,
    pub metrics: PipelineMetrics,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PipelineStatus {
    Running,
    Stopped,
    Error,
    Paused,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PipelineMetrics {
    pub records_processed: u64,
    pub bytes_processed: u64,
    pub errors_count: u64,
    pub last_sync_at: Option<DateTime<Utc>>,
    pub avg_latency_ms: f64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FluvioTopic {
    pub name: String,
    pub partitions: u32,
    pub replication_factor: u16,
    pub retention_time: Option<u64>,
    pub compression: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DataRecord {
    pub id: Uuid,
    pub topic: String,
    pub partition: u32,
    pub offset: u64,
    pub timestamp: DateTime<Utc>,
    pub data: serde_json::Value,
    pub metadata: HashMap<String, String>,
}
